﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TruckWatch.Cloud.DAL;
using TruckWatch.Cloud.Models;
using TruckWatch.Cloud.BusinessLogic;
using TruckWatch.Cloud.Models.ViewModels;

namespace Mvc30WebRole1.Controllers
{
    public class DriversController : Controller
    {

        public ActionResult Index()
        {
            return RedirectToAction("DriverList");
        }

        public ActionResult DriverList()
        {
            List<DriverSummary> drivers = DriverDAL.GetAllDriverSummaries();
            return View(drivers);
        }

        public ActionResult Detail(int id)
        {
            DriverModel driver = DriverBL.GetDriverModelbyId(id, true);
            return View(driver);
        }

        #region Update Driver

        [HttpPost]
        public ActionResult UpdateDriver(int id, string name, string truckType)
        {
            DriverDAL.UpdateDriver(id, name, truckType);

            return RedirectToAction("DriverList");
        }

        #endregion

        #region Add Driver

        public ActionResult AddDriver()
        {

            return View();
        }

        [HttpPost]
        public ActionResult AddDriver(DriverNEF driver)
        {
            DriverDAL.AddDriver(driver);

            return RedirectToAction("DriverList");
        }

        #endregion

        #region Add Driver Trip

        public ActionResult AddDriverTrip(int id)
        {
            TruckWatch.Cloud.Models.EF.Trip newTrip = new TruckWatch.Cloud.Models.EF.Trip();
            newTrip.DriverId = id;
            newTrip.ScheduledDeparture = DateTime.UtcNow;
            newTrip.ScheduledArrival = newTrip.ScheduledDeparture.AddDays(2);

            //
            DriverNEF driver = DriverDAL.GetDriverById(id);
            ViewBag.DriverName = driver.Name;

            //
            return View(newTrip);
        }


        [HttpPost]
        public ActionResult AddDriverTrip(TruckWatch.Cloud.Models.EF.Trip trip)
        {
            // Validate
            this.ValidateModel(trip);

            TripBL.AddTrip(trip);

            return RedirectToAction("Detail", "Drivers", new { Id = trip.Driver.Id });
        }

        #endregion

        #region Delete Driver Trip

        public ActionResult DeleteDriverTrip(int tripId, int driverId)
        {
            TripBL.DeleteTrip(tripId);

            return RedirectToAction("Detail", "Drivers", new { Id = driverId });
        }
        #endregion
    }
}
